package com.slf.engine.services.impl;

import java.io.IOException;

import org.apache.log4j.Priority;

import com.slf.engine.bo.Order;
import com.slf.engine.bo.WaitBean;
import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.ITjfxConnService;
import com.slf.engine.services.helper.ServiceHelper;

public class TjfxConnServiceImpl extends BaseService implements ITjfxConnService {

	private static final org.apache.log4j.Logger log  = org.apache.log4j.Logger.getLogger(TjfxConnServiceImpl.class);
	
	/**
	 * socket登录对方服务器
	 * @return
	 * @throws IOException 
	 */
	public boolean loginServer() 
	{
		String msg = ServiceHelper.makeMsg(ServiceHelper.makeLoginBody().toString()).toString();
		try {
			sendMsg(msg);
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, "上游登录请求连接失败，3秒后重试");
		}
		return false;
	}
	/**
	 * 注销请求
	 * @throws IOException 
	 */
	public boolean logoutServer() throws IOException
	{
//			client = new Socket(SysContants.TJFX_IP,SysContants.TJFX_PORT);
//		if(!loginServer(client))
//		{
//			client = SocketUtil.getInStantClient();
//			return logoutServer(client);
//		}
		String msg = ServiceHelper.makeMsg(ServiceHelper.makeLogoutBody().toString()).toString();
		sendMsg(msg);
	
			
		return false;
	}
	
	/**
	 * 交易充值
	 * @throws IOException 
	 */
	public boolean sendBusMsg(Order order)
	{
		String msg = ServiceHelper.makeMsg(ServiceHelper.makeBusBody(order).toString()).toString();
		try {
			sendMsg(msg);
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, "上游发送交易异常，充值失败");
		}
		return false;
	}
	
	public Order backBusMsg(String back)
	{
		Order order = null;
		String orderId = back.substring(12, 32).trim();
		String code = back.substring(32, 34);
		String ye = back.substring(34, 44);
//		String date = back.substring(44, 52);
//		String time = back.substring(52, 58);
		String yyslsh = back.substring(58, 78);
		String remark = back.substring(78, 118);
		synchronized (SysContext.QUERY_MAP) {
			if(SysContext.QUERY_MAP.containsKey(orderId))
			{
				WaitBean waitBean = SysContext.QUERY_MAP.get(orderId);
				waitBean.removeFromList();
				order = waitBean.getOrder();
			}
		}
		if(order != null)
		{
			if("00".equals(code) || "19".equals(code))
			{
				order.setClbz(SysContants.CZ_SUCCESS);
				order.setCljg("充值成功，当前余额：" + Integer.parseInt(ye)/100);
				order.setYyslsh(yyslsh);
			}else if("01".equals(code) || "02".equals(code) || "03".equals(code) || "04".equals(code)
					 || "05".equals(code) || "06".equals(code) || "09".equals(code) || "10".equals(code)
					 || "11".equals(code)){
				order.setClbz(SysContants.CZ_FAILURE);
				order.setCljg("充值失败，code:" + code + " remark:" + remark);
			}else{
				order.setClbz(SysContants.CZ_UNKNOW);
				order.setCljg("充值异常，code:" + code + " remark:" + remark);
			}
		}else{
			return null;
		}
		return order;
	}
	
	
	/**
	 * 交易查询
	 * @param msg
	 * @return
	 * @throws IOException 
	 */
	public boolean sendQueryMsg(Order order)
	{
		String msg = ServiceHelper.makeMsg(ServiceHelper.makeQueryBody(order).toString()).toString();
		try {
			sendMsg(msg);
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, "上游发送查询异常");
		}
		return false;
	}
	
	public Order backQueryMsg(String back)
	{
		Order order = null;
		String orderId = back.substring(12, 32).trim();
		String code = back.substring(32, 34);
//		String bus = back.substring(34, 36);
		String result = back.substring(36, 38);
		String yyslsh = back.substring(38, 58);
		
		synchronized (SysContext.QUERY_MAP) {
			if(SysContext.QUERY_MAP.containsKey(orderId))
			{
				WaitBean waitBean = SysContext.QUERY_MAP.get(orderId);
				waitBean.removeFromList();
				order = waitBean.getOrder();
			}
		}
		if("00".equals(code))
		{
			if("00".equals(result))
			{
				//成功
				order.setYyslsh(yyslsh);
				order.setClbz(SysContants.CZ_SUCCESS);
				order.setCljg("充值成功，查询返回结果：" + result);
			}else if("16".equals(code)){
				//失败
				order.setClbz(SysContants.CZ_FAILURE);
				order.setCljg("充值失败，查询返回结果code:" + code);
			}else{
				//未确定
				order.setClbz(SysContants.CZ_UNKNOW);
				order.setCljg("充值异常结果，请平台人员校验code:" + code);
			}
		}else{
			order.setClbz(SysContants.CZ_UNKNOW);
			order.setCljg("上游接收交易异常，请平台人员校验");
		}
		
		return order;
	}
	
	
//	public void closeServer()
//	{
//		log.log(Priority.INFO, "与天津蜂星服务断开连接");
//		try {
//			if(in != null)
//			{
//				in.close();
//			}
//			if(out != null)
//			{
//				out.close();
//			}
//			if(SocketUtil.getInStantClient() != null)
//			{
//				SocketUtil.getInStantClient().close();
//			}
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//		}
//	}
}
